/*********************
* Author:Dzlua
* QQ:505544956
* time:2016/08/19
*/
#include "HorizontalLayout.h"
#include "../edui.h"

EDUI_NAMESPACE_BEGIN

HorizontalLayout::HorizontalLayout() : m_bCenter(false)
{}

HorizontalLayout::~HorizontalLayout()
{}

HorizontalLayout* HorizontalLayout::GetInterface()
{
    HorizontalLayout* horizontalLayout = new HorizontalLayout;
#ifdef EDUI_TEST_NEW_DELETE
    ++m_newdeleteNum;
    Log("[HorizontalLayout] new horizontalLayout : %d", horizontalLayout);
#endif
#ifdef EDUI_USE_LUAPLUS
    LuaMgrPtr->Add("HorizontalLayout", horizontalLayout);
#endif
    return horizontalLayout;
}

void HorizontalLayout::Release()
{
    this->Clear();
#ifdef EDUI_TEST_NEW_DELETE
    --m_newdeleteNum;
    Log("[HorizontalLayout] delete horizontalLayout : %s, %d", this->GetName(), this);
#endif
#ifdef EDUI_USE_LUAPLUS
    LuaMgrPtr->Remove(this);
#endif
    delete this;
}

HorizontalLayout* HorizontalLayout::ToHorizontalLayout(Control* ctrl)
{
    return dynamic_cast<HorizontalLayout*>(ctrl);
}

bool HorizontalLayout::IsCenter() const
{
    return m_bCenter;
}

void HorizontalLayout::SetCenter(bool bCenter)
{
    m_bCenter = bCenter;
}

void HorizontalLayout::AutoLayout()
{
    if (m_wnd->GetRoot() == this){
        m_rc.Reset(0, 0, m_wnd->GetWidth(), m_wnd->GetHeight());
    }
    Control::AutoLayout();

    if (m_items->size() == 0) return;

    Rect rc = m_rc;
    rc.x += m_rcInset.l;
    rc.y += m_rcInset.t;
    rc.w -= (m_rcInset.l + m_rcInset.r);
    rc.h -= (m_rcInset.t + m_rcInset.b);

    int fixed = 0;
    int autonum = 0;
    int total = 0;
    for (ItemMap::iterator it = m_items->begin(); it != m_items->end();  ++it){
        if ((*it) && (*it)->IsVisible() && !(*it)->IsFloat()){
            Size sz = (*it)->GetFixedSize();
            if (sz.w == 0) ++autonum;
            Rect1 padding = (*it)->GetPadding();
            fixed += sz.w + padding.l + padding.r;
            ++total;
        }
    }

    if (total > 1) fixed += (total - 1) * m_childPadding.w;

    int expand = 0;
    int expandoffset = 0;
    if (autonum > 0){
        expand = (rc.w - fixed) / autonum;
        expandoffset = (rc.w - fixed) - autonum * expand;
    }
    if (expand < 0){
        expand = 0;
        expandoffset = 0;
    }

    Rect rctmp;
    int posX = rc.x; int posY = 0;
    int w,h;
    for (ItemMap::iterator it = m_items->begin(); it != m_items->end();  ++it){
        if ((*it) && (*it)->IsVisible()){
            if ((*it)->IsFloat()){
                (*it)->SetRect(&rc);
                (*it)->AutoLayout();
            } else {
                Size sz = (*it)->GetFixedSize();
                if (sz.w == 0){
                    sz.w = expand + expandoffset;
                    expandoffset = 0;
                }
                Rect1 padding = (*it)->GetPadding();
                posY = (!m_bCenter || (sz.h == 0) || (sz.h >= rc.h)) ? rc.y : (rc.y + (rc.h - sz.h)/2);

                w = sz.w + padding.l + padding.r;
                h = rc.h;
                if (posX + w > rc.x + rc.w) w = rc.x + rc.w - posX;
                if (posY + h > rc.y + rc.w) h = rc.y + rc.h -posY;

                rctmp.Reset(posX, posY, w, h);

                (*it)->SetRect(&rctmp);
                (*it)->AutoLayout();

                posX += sz.w + m_childPadding.w + padding.l + padding.r;
            }
        }
    }
}

void HorizontalLayout::SetAttribute(const char* name, const char* value)
{
    if (0 == strcmp(name, "center")){
        bool b = ParserBool(value, true);
        this->SetCenter(b);
    } else if (0 == strcmp(name, "childpadding")){
        ParserInt(value, &m_childPadding.w); 
    } else {
        Container::SetAttribute(name, value);
    }
}

EDUI_NAMESPACE_END
